/*
 *  Copyright 2019-2023 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.stock.domain.TblKline;
import me.zhengjie.stock.domain.TblStockPools;
import me.zhengjie.stock.domain.vo.LongLetStockQueryCriteria;
import me.zhengjie.stock.mapper.TblStockPoolsMapper;
import me.zhengjie.stock.service.TblKlineService;
import me.zhengjie.stock.service.TblStockPoolsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author dolojia
 * @description 服务实现
 * @date 2024-05-04
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class TblStockPoolsServiceImpl extends ServiceImpl<TblStockPoolsMapper, TblStockPools> implements TblStockPoolsService {

    @Autowired
    private TblKlineService klineService;

    @Override
    public void updateLongLetStock(LongLetStockQueryCriteria criteria) {
        LambdaQueryWrapper<TblStockPools> stockPoolsQueryWrapper = new QueryWrapper<TblStockPools>().lambda();
        super.remove(stockPoolsQueryWrapper);
        LambdaQueryWrapper<TblKline> queryOneWrapper = new QueryWrapper<TblKline>().lambda();
        queryOneWrapper.orderByDesc(TblKline::getTradingDate);
        queryOneWrapper.last("limit 1");
        //获取日期最新的一条数据
        TblKline tblKline = klineService.getOne(queryOneWrapper);
        LambdaQueryWrapper<TblKline> queryWrapper = new QueryWrapper<TblKline>().lambda();
        //涨幅大于等于5小于11的数据
        queryWrapper.ge(TblKline::getChangeRate, criteria.getChangeRate());
        queryWrapper.eq(TblKline::getTradingDate, tblKline.getTradingDate());
        queryWrapper.orderByDesc(TblKline::getTradingDate);
        List<TblKline> tblKlineList = klineService.list(queryWrapper);
        log.info("涨幅大于等于5的数据{}", tblKlineList);
        List<TblStockPools> stockPoolsList = new ArrayList<>();
        for (TblKline kline : tblKlineList) {
            LambdaQueryWrapper<TblKline> queryWrapper2 = new QueryWrapper<TblKline>().lambda();
            //涨幅大于等于5小于11的数据
            queryWrapper2.ne(TblKline::getTradingDate, tblKline.getTradingDate());
            queryWrapper2.eq(TblKline::getCode, kline.getCode());
            queryWrapper2.eq(TblKline::getName, kline.getName());
            queryWrapper2.orderByDesc(TblKline::getTradingDate);
            queryWrapper2.last("limit 1");
            //获取前一天的数据
            TblKline tblKlineBefor = klineService.getOne(queryWrapper2);
            if (tblKlineBefor == null) {
                continue;
            }
            BigDecimal aountMultiple = kline.getTradingAmount().divide(tblKlineBefor.getTradingAmount(), 2).setScale(2);
            //交易额大于2倍的数据入大长腿池
            if (aountMultiple.compareTo(new BigDecimal(criteria.getAountMultiple())) > 0) {
                log.info("涨幅大于等于5且成交量放大{}倍以上数据{}", aountMultiple, tblKlineBefor);
                TblStockPools tblStockPools = new TblStockPools();
                tblStockPools.setName(kline.getName());
                tblStockPools.setCode(kline.getCode());
                tblStockPools.setTradingDate(kline.getTradingDate());
                tblStockPools.setChangeRate(kline.getChangeRate());
                tblStockPools.setAmountMultiple(aountMultiple);
                BigDecimal volumeMultiple = new BigDecimal(kline.getTradingVolume()).divide(new BigDecimal(tblKlineBefor.getTradingVolume()), 2);
                tblStockPools.setVolumeMultiple(volumeMultiple);
                stockPoolsList.add(tblStockPools);
            }
        }
        super.saveBatch(stockPoolsList);
    }

}